package com.glela.goods.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.glela.activ.mapper.OActivContentExpressMapper;
import com.glela.activ.mapper.OActivContentMoneyMapper;
import com.glela.activ.mapper.OActivGiftProductMapper;
import com.glela.activ.model.OActivContentExpress;
import com.glela.activ.model.OActivContentMoney;
import com.glela.activ.model.OActivGiftProduct;
import com.glela.common.model.ProductTag;
import com.glela.common.util.DateUtil;
import com.glela.goods.mapper.ActivConditionSalesAmountMapper;
import com.glela.goods.mapper.ActivRangeProductMapper;
import com.glela.goods.mapper.ActivityMapper;
import com.glela.goods.mapper.GoodsMapper;
import com.glela.goods.mapper.InventoryMapper;
import com.glela.goods.model.Activity;
import com.glela.goods.model.GoodsPrice;
import com.glela.goods.model.Inventory;
import com.glela.goods.model.vo.goods.ActivConditionSalesAmount;
import com.glela.goods.model.vo.goods.ActivRangeProduct;
import com.glela.goods.model.vo.goods.ActiveGoods;
import com.glela.goods.model.vo.goods.GoodSerialVo;
import com.glela.goods.model.vo.goods.GoodsAndTitleVo;
import com.glela.goods.service.ActivityService;
import com.glela.goods.service.GoodsService;
import com.glela.platform.mapper.UserMapper;
import com.glela.platform.model.User;
import com.glela.platform.model.UserInfo;
import com.glela.platform.service.UserService;

/**
 * @author liujie
 * @2016年11月24日
 */
@Service("activityService")
public class ActivityServiceImpl implements ActivityService {
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ActivRangeProductMapper activRangeProductMapper;
	
	@Autowired
	private ActivConditionSalesAmountMapper activConditionSalesAmountMapper;
	
	@Autowired
	private GoodsMapper goodsMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private ActivityMapper activityMapper;
	
	@Autowired
	private OActivContentMoneyMapper oActivContentMoneyMapper;
	
	@Autowired
	private OActivGiftProductMapper oActivGiftProductMapper;
	
	@Autowired
	private OActivContentExpressMapper oActivContentExpressMapper;
	
	@Autowired
	private InventoryMapper inventoryMapper;
	
	@Override
	public Activity selectByPrimaryKey(Long id) {
		return activityMapper.selectByPrimaryKey(id);
	}

	@Override
	public Activity selectIngByPrimaryKey(Long id) {
		return activityMapper.selectIngByPrimaryKey(id);
	}

	@Override
	public int subInventoryNumById(long id, int quantity, long inventoryId) {
		return activityMapper.subInventoryNumById(id, quantity, inventoryId);
	}

	
	@Override
	public GoodsAndTitleVo selectActiveProductsListById(long marketingActivitiesId, long userId, int page, int pageSize,int pageNum) {
		GoodsAndTitleVo goodsAndTitleVo = new GoodsAndTitleVo();
		ArrayList<ActiveGoods> goodList = new ArrayList<>();
		ArrayList<Object> titleList = new ArrayList<>();
		//用户是否为店主  不是店主 只显示价格  店主显示能赚佣金  查询一次
		User user = userMapper.selectByPrimaryKey((long) userId);
	
			List<GoodSerialVo> gList = goodsMapper.selectOneGoodByIdList(marketingActivitiesId,page,pageSize);
			for (GoodSerialVo g : gList) {
				
				if (null != g) {
					GoodsPrice goodPrice = new GoodsPrice();
					ActiveGoods activeGoods = new ActiveGoods();
						if(user != null && StringUtils.isNotEmpty(user.getRecommendFrom())){
							UserInfo userInfo = this.userService.selectUserInfoByPersionId(user.getRecommendFrom());//推荐人身份
							User recondFormUser = userMapper.selectByPrimaryKey(userInfo.getUserId());
							 goodPrice = goodsService.getGoodsViewPriceForGoods(recondFormUser, g.getGoodId());
							
						}else{
							 goodPrice = goodsService.getGoodsViewPriceForGoods(user, g.getGoodId());
						}
						
						if(goodPrice != null){
							if(user != null){
								if(user.getRoleId() != 1 && user.getRoleId() != 2){
								activeGoods.setCommission(goodPrice.getCommission());   //店主显示可赚佣金
								}
							}
							activeGoods.setSuggestedPrice(goodPrice.getMarketPirce());  
							activeGoods.setLowestPrice(goodPrice.getSalePrice());
						}
						
					Inventory inventory = inventoryMapper.selectInventoryByPid(goodPrice.getProductId());
					if(inventory != null){
						activeGoods.setInventory(inventory.getId());
						activeGoods.setInventoryNum(inventory.getInventoryNum());
					}
					activeGoods.setSelfSupport(g.getSelfSupport()); 
					activeGoods.setProductId(goodPrice.getProductId());
					activeGoods.setGoodId(g.getGoodId());
					activeGoods.setLabelName(g.getLabelName());
					activeGoods.setName(g.getName());
					activeGoods.setNavigateImage(g.getNavigateImage());
					String nowTime = DateUtil.currentStringDate();
					//商品标签
					List<ProductTag> ptList = new ArrayList<ProductTag>();
					//状态
					Map<String,Object> ltsMap = this.goodsMapper.selectNearlyLimitTimeSale(g.getGoodId(),goodPrice.getProductId(),nowTime);
					if(ltsMap != null){
						ProductTag p = new ProductTag();
						p.setColor("#ff4b7a");
						try {
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//抢购开始时间
							Date fullBeginTime = sdf.parse(ltsMap.get("fullBeginTime").toString());
							// 系统当前时间
							long nowTimeMillis = System.currentTimeMillis();
							if (fullBeginTime.getTime() > nowTimeMillis) {// 预热中
								p.setLabel("预热");
							} else {
								p.setLabel("特卖");
							}
						} catch (Exception e) {
							
						}
						ptList.add(p);
					}
					//属性
					if (g.getSelfSupport() == 0) {
						ProductTag pT = new ProductTag();
						pT.setColor("#ff4b7a");
						pT.setLabel("自营");
						ptList.add(pT);
					}
					activeGoods.setProductTags(ptList);
					goodList.add(activeGoods);
				}
			}
		goodsAndTitleVo.setPage(pageNum);
		goodsAndTitleVo.setPageSize(pageSize);
		goodsAndTitleVo.setGoodsList(goodList);
		//只有首页显示 活动标题
		//活动标题三个表中查询  
		if(pageNum == 1){
			List<OActivContentMoney> contentMoney = oActivContentMoneyMapper.selectOActivContentMoneyByActivId(marketingActivitiesId);
			if(contentMoney != null){
				for (OActivContentMoney oActivContentMoney : contentMoney) {
					titleList.add(oActivContentMoney.getActivTitle()+";");
				}
			}
			
			List<OActivGiftProduct> giftList = oActivGiftProductMapper.selectOActivGiftProductByActivId(marketingActivitiesId);
			if(giftList != null && giftList.size() > 0){
				for (OActivGiftProduct gift : giftList) {
					titleList.add(gift.getActivTitle()+";");
				}
			}
			
			List<OActivContentExpress> expressList = oActivContentExpressMapper.selectOActivContentExpressByActivId(marketingActivitiesId);
			if(expressList != null && expressList.size() > 0){
				for (OActivContentExpress express : expressList) {
					titleList.add(express.getActivTitle()+";");
				}
			}
			goodsAndTitleVo.setTitleList(titleList);
		}
		return goodsAndTitleVo;
	}
	
	

	/*@Override
	public GoodsAndTitleVo selectActiveProductsListById(long marketingActivitiesId, long userId, int page, int pageSize) {
		GoodsAndTitleVo goodsAndTitleVo = new GoodsAndTitleVo();
		ArrayList<ActiveGoods> goodList = new ArrayList<>();
		ArrayList<Object> titleList = new ArrayList<>();
		//用户是否为店主  不是店主 只显示价格  店主显示能赚佣金  查询一次
		User user = userMapper.selectByPrimaryKey((long) userId);
		
		//活动 id
		List<ActivRangeProduct> activeList = activRangeProductMapper.selectByActiveId(marketingActivitiesId);
		HashSet<Long> hashSet = new HashSet<>();
		//根据活动id查询商品id 并将商品id 去重
		for (ActivRangeProduct activRangeProduct : activeList) {
			hashSet.add(activRangeProduct.getGoodsId());
		}
				
			ArrayList<Long> list = new ArrayList<>();
			list.addAll(hashSet);
			PageHelper.startPage(page, pageSize);
			List<GoodSerialVo> gList = goodsMapper.selectOneGoodByIdList(list);
			for (GoodSerialVo g : gList) {
				
				if (null != g) {
					GoodsPrice goodPrice = new GoodsPrice();
					ActiveGoods activeGoods = new ActiveGoods();
						if(user != null && StringUtils.isNotEmpty(user.getRecommendFrom())){
							UserInfo userInfo = this.userService.selectUserInfoByPersionId(user.getRecommendFrom());//推荐人身份
							User recondFormUser = userMapper.selectByPrimaryKey(userInfo.getUserId());
							 goodPrice = goodsService.getGoodsViewPriceForGoods(recondFormUser, g.getGoodId());
							
						}else{
							 goodPrice = goodsService.getGoodsViewPriceForGoods(user, g.getGoodId());
						}
						
						if(goodPrice != null){
							if(user != null){
								if(user.getRoleId() != 1 && user.getRoleId() != 2){
								activeGoods.setCommission(goodPrice.getCommission());   //店主显示可赚佣金
								}
							}
							activeGoods.setSuggestedPrice(goodPrice.getMarketPirce());  
							activeGoods.setLowestPrice(goodPrice.getSalePrice());
						}
						
					Inventory inventory = inventoryMapper.selectInventoryByPid(goodPrice.getProductId());
					if(inventory != null){
						activeGoods.setInventory(inventory.getId());
						activeGoods.setInventoryNum(inventory.getInventoryNum());
					}
					activeGoods.setSelfSupport(g.getSelfSupport()); 
					activeGoods.setProductId(goodPrice.getProductId());
					activeGoods.setGoodId(g.getGoodId());
					activeGoods.setLabelName(g.getLabelName());
					activeGoods.setName(g.getName());
					activeGoods.setNavigateImage(g.getNavigateImage());
					//商品标签
					List<ProductTag> ptList = new ArrayList<ProductTag>();
					//状态
					Map<String,Object> ltsMap = this.goodsMapper.selectNearlyLimitTimeSale(g.getGoodId(),goodPrice.getProductId());
					if(ltsMap != null){
						ProductTag p = new ProductTag();
						p.setColor("#ff4b7a");
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//抢购开始时间
						try {
							Date fullBeginTime = sdf.parse(ltsMap.get("fullBeginTime").toString());
							long t = DateUtil.getMinuteDiff(new Date(),fullBeginTime);// 比较当前时间与抢购开始时间的分钟差
							if(t >= 0){// 预热中
								p.setLabel("预热");
							} else {
								p.setLabel("特卖");
							}
						} catch (Exception e) {
							
						}
						ptList.add(p);
					}
					//属性
					if (g.getSelfSupport() == 0) {
						ProductTag pT = new ProductTag();
						pT.setColor("#ff4b7a");
						pT.setLabel("自营");
						ptList.add(pT);
					}
					activeGoods.setProductTags(ptList);
					goodList.add(activeGoods);
				}
			}
		goodsAndTitleVo.setPage(page);
		goodsAndTitleVo.setPageSize(pageSize);
		goodsAndTitleVo.setGoodsList(goodList);
		//只有首页显示 活动标题
		//活动标题三个表中查询  
		if(page == 1){
			List<OActivContentMoney> contentMoney = oActivContentMoneyMapper.selectOActivContentMoneyByActivId(marketingActivitiesId);
			if(contentMoney != null){
				for (OActivContentMoney oActivContentMoney : contentMoney) {
					titleList.add(oActivContentMoney.getActivTitle()+";");
				}
			}
			
			List<OActivGiftProduct> giftList = oActivGiftProductMapper.selectOActivGiftProductByActivId(marketingActivitiesId);
			if(giftList != null && giftList.size() > 0){
				for (OActivGiftProduct gift : giftList) {
					titleList.add(gift.getActivTitle()+";");
				}
			}
			
			List<OActivContentExpress> expressList = oActivContentExpressMapper.selectOActivContentExpressByActivId(marketingActivitiesId);
			if(expressList != null && expressList.size() > 0){
				for (OActivContentExpress express : expressList) {
					titleList.add(express.getActivTitle()+";");
				}
			}
			goodsAndTitleVo.setTitleList(titleList);
		}
		return goodsAndTitleVo;
	}*/
}
